<?php

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;

class UpdateAdminRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true; // 在实际应用中，你可能需要根据用户权限来判断
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'username' => 'sometimes|string|max:255|unique:admins,username,' . $this->admin->id,
            'email' => 'sometimes|email|max:255|unique:admins,email,' . $this->admin->id,
            'password' => ['sometimes', 'string', Password::defaults()],
            'name' => 'sometimes|string|max:255',
            'phone' => 'nullable|string|max:20',
            'avatar' => 'nullable|string|max:255',
            'status' => 'sometimes|boolean',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息。
     */
    public function messages(): array
    {
        return [
            'username.unique' => '用户名已存在',
            'email.email' => '邮箱格式不正确',
            'email.unique' => '邮箱已存在',
        ];
    }
}
